package com.atguigu.boot_redis05_cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * 需要计算的属性重写他的get方法，每次刷新他都会重新计算，然后显示到页面上
 * @author gcq
 * @Create 2021-01-03
 */
public class Cart {
    /**
     * 购物车中商品项
     */
    private List<CartItem> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 购物车中全部商品总价格
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格（优惠卷等等）
     */
    private BigDecimal reduce = new BigDecimal("0");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        return countNum;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    /**
     * 拿到购物车中购物项总数量
     * @return
     */
    public Integer getCountType() {
        int count = 0;
        if (items !=null && items.size() > 0) {
            // 遍历购物项
            for (CartItem item : items) {
                count+=1;
            }
        }
        return count;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    /**
     * 拿到购物车中选中的购物项的总金额
     * @return
     */
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 1、计算购物项总价
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                // 商品选中后计算价格
                if (item.getCheck()) {
                    // 拿到购物车中单个商品的总金额
                    BigDecimal totalPrice = item.getTotalPrice();
                    // 添加到购物总价中
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 2、减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}